﻿using QuanLyThuVien.Supporter;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyThuVien.GUI
{
    public partial class GUIKetNoiServer : DevComponents.DotNetBar.Office2007Form
    {
        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public GUIKetNoiServer()
        {
            InitializeComponent();
        }

        //----------------------------------------------------------------------
        //Desc: load form
        //----------------------------------------------------------------------
        private void GUIKetNoiServer_Load(object sender, EventArgs e)
        {
            _cboAuthentication.SelectedItem = _cboAuthentication.Items[0];
            DisplayInstanceSqlServer();
        }

        //----------------------------------------------------------------------
        //Desc: hiện thị tên server sql
        //----------------------------------------------------------------------
        public void DisplayInstanceSqlServer()
        {
            List<string> serverNames = DatabaseManager.GetServerName();

            foreach (string serverName in serverNames)
                _cboServerName.Items.Add(serverName);

            if (_cboServerName.Items.Count > 1)
                _cboServerName.SelectedItem = _cboServerName.Items[1];
        }

        //----------------------------------------------------------------------
        //Desc: xử lý sự kiện thay đổi quyền đăng nhập
        //----------------------------------------------------------------------
        private void _cboAuthentication_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_cboAuthentication.SelectedItem == _cboAuthentication.Items[0])
            {
                _lblUser.Enabled = false;
                _lblPassword.Enabled = false;
                _txtUserName.Enabled = false;
                _txtPassword.Enabled = false;
                _chbHienMatKhau.Enabled = false;
            }
            else
            {
                _lblUser.Enabled = true;
                _lblPassword.Enabled = true;
                _txtUserName.Enabled = true;
                _txtPassword.Enabled = true;
                _chbHienMatKhau.Enabled = true;
            }
        }

        //----------------------------------------------------------------------
        //Desc: xử lý sự kiện click button connect
        //----------------------------------------------------------------------
        private void btnKetNoiServer_Click(object sender, EventArgs e)
        {
            string strConn;
            if (_cboAuthentication.SelectedItem == _cboAuthentication.Items[0])
                strConn = DatabaseManager.CreateConnectionString(_cboServerName.Text, "master", "True");
            else
                strConn = DatabaseManager.CreateConnectionString(_cboServerName.Text, "master", _txtUserName.Text, _txtPassword.Text, "False");

            DatabaseConnection lastMasterConn = null;
            if (DatabaseManager.MasterConnection != null && DatabaseManager.MasterConnection.Open())
            {
                DatabaseManager.MasterConnection.Close();
                lastMasterConn = DatabaseManager.MasterConnection;
            }

            DatabaseManager.MasterConnection = new DatabaseConnection(strConn);
            if (DatabaseManager.MasterConnection != null && DatabaseManager.MasterConnection.Open())
            {
                DatabaseManager.MasterConnection.Close();
                MessageBox.Show("Kết nối thành công.");
                if (_cboAuthentication.SelectedItem == _cboAuthentication.Items[0])
                    DatabaseManager.MasterConnection.SetContent(_cboServerName.Text, "master", "", "", "True");
                else
                    DatabaseManager.MasterConnection.SetContent(_cboServerName.Text, "master", _txtUserName.Text, _txtPassword.Text, "False");
                this.Close();
            }
            else
            {
                MessageBox.Show("Kết nối thất bại");
                if (lastMasterConn != null)
                    DatabaseManager.MasterConnection = lastMasterConn;
            }
        }

        //----------------------------------------------------------------------
        //Desc: xử lý sự kiện click button cancel
        //----------------------------------------------------------------------
        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch
            { }
        }

        //----------------------------------------------------------------------
        //Desc: xử lý sự kiện check HienMatKhau
        //----------------------------------------------------------------------
        private void chbHienMatKhau_CheckedChanged(object sender, EventArgs e)
        {
            if (_chbHienMatKhau.Checked)
                _txtPassword.UseSystemPasswordChar = false;
            else
                _txtPassword.UseSystemPasswordChar = true;
        }

        //----------------------------------------------------------------------
        //Desc: xử lý sự kiện key press
        //----------------------------------------------------------------------
        private void Special_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Utils.IsSpecialKey(e.KeyChar))
                e.Handled = true;
            else
                e.Handled = false;
        }
    }
}
